<!--预警记录页面-->
<template>
  <div class="app-container">
    <el-form ref="form" inline :model="form" class="form-options" label-width="100px">
      <el-form-item label="事件类型" prop="eventType">
        <el-autocomplete
          ref="eventTypeRef"
          v-model="form.eventType"
          value-key="label"
          placeholder="请输入事件类型"
          :fetch-suggestions="querySearchEventType"
          :trigger-on-focus="true"
          clearable
          @select="handleSelectEventType"
          @clear="clickCleareventTypeRef"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="devType">
        <el-select v-model="form.devType" placeholder="请选择设备类型"  filterable clearable>
          <el-option
            v-for="item in devTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="故障时间" class="long-content" prop="inTime">
        <el-date-picker
          v-model="form.time"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          align="right"
        />
      </el-form-item>

      <div class="btns">
        <el-button type="primary" @click="onQuery">查询</el-button>
        <el-button @click="resetFormSearch('form')">重置</el-button>
      </div>
    </el-form>
    <!--列表-->
    <el-table
      v-loading="loading"
      :data="tableData"
      header-cell-class-name="table-header"
      style="width: 100%;margin-top: 20px"
    >
      <el-table-column
        label="序号"
        type="index"
        width="50"
      />
      <el-table-column
        prop="eventNo"
        label="事件编号"
      />
      <el-table-column
        prop="devType"
        label="设备类型"
      >
        <template slot-scope="scope">
          <div>{{transDevName(scope.row.devType)}}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="eventType"
        label="事件类型"
      />
      <el-table-column
        prop="warnTime"
        label="预警时间"
      />
      <el-table-column
        prop="removeTime"
        label="消除时间"
      />
      <el-table-column
        prop="warnDuration"
        label="预警时长"
      />
      <el-table-column
        prop="eventDescribe"
        label="事件描述"
      />
      <el-table-column
        prop="director"
        label="运维责任人"
      />
    </el-table>

    <!--分页-->
    <div style="text-align: right;margin-top: 15px">
      <el-pagination
        background
        :current-page="currentPage"
        :page-sizes="[10, 15, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import {
  getDevList
} from '@/api/deviceMonitor';
import {
  getQueryEventRecord
} from '@/api/warningRecord';

export default {
  name: 'DeviceMonitor',
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      eventTypeOptions:[{
        value: '故障',
        label: '故障'
      }, {
        value: '信号中断',
        label: '信号中断'
      },{
        value: '超限',
        label: '超限'
      }],
      devTypeOptions: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      form: {
        eventType: '',
        devType: '',
        time: ''
      },
      loading: false,
      tableData: []
    }
  },
  created() {
    this.getDevListFun();
    this.onQuery();
  },
  methods: {
    // 分页
    handleSizeChange: function(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getTableData();
    },
    handleCurrentChange: function(val) {
      this.currentPage = val;
      this.getTableData();
    },
    // 翻译-设备类型
    transDevName(val) {
      return this.devTypeOptions.find(el => el.value == val).label || '';
    },
    // 获取事件类型
    querySearchEventType: function(queryString, cb) {
      const r = this.eventTypeOptions;
      var rs = queryString ? r.filter(this.createFilter(queryString)) : r;
      cb(rs);
    },
    handleSelectEventType(item) {
      this.form.eventType = item.label;
    },
    clickCleareventTypeRef(){
      this.$nextTick(() => {
        if (this.$refs.eventTypeRef) {
          this.$refs.eventTypeRef.activated = true;
        }
      });
    },
    // 模糊建议公共方法
    createFilter(queryString) {
      return (rr) => {
        return (rr.name.toLowerCase().indexOf(queryString.toLowerCase()) != -1);
      }
    },
    // 接口-设备类型
    getDevListFun() {
      getDevList({
        page: 1,
        rows: 100
      }).then(res => {
        const { resultData } = res;
        resultData.forEach(element => {
          this.devTypeOptions.push({
            value: element.id,
            label: element.category
          })
        });
      })
    },
    // 查询
    onQuery() {
      console.log('查询', this.form)
      this.currentPage = 1;
      this.getTableData();
    },
    // 查询列表数据
    getTableData() {
      this.loading = true;
      const { time }  = this.form;
      if(time && time.length> 0){
        this.$set(this.form, 'startTime', time[0]);
        this.$set(this.form, 'endTime', time[1]);
      } else {
        this.$set(this.form, 'startTime', '');
        this.$set(this.form, 'endTime', '');
      }
      const params = {
        page: this.currentPage,
        rows: this.pageSize,
        ...this.form
      };
      getQueryEventRecord(params).then(res => {
        this.tableData = res.resultData;
        this.total = res.total;
        this.loading = false;
      }).catch(err => {
        this.loading = false;
      })
    },
    // 新增
    handleAdd() {
      this.dialogVisible = true;
      this.resetForm("diaForm");
    },
    // 新增提交
    onSubmit() {
      this.$refs["diaForm"].validate((valid) => {
        if (valid) {
          const { name } = this.diaForm;
          getCheckAdd({name}).then(res => {
            this.$message({
              message: '保存成功',
              type: 'success'
            })
            this.onQuery();
            this.dialogVisible = false;
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 搜索重置
    resetFormSearch(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
        this.$refs[formName].clearValidate();
        this.currentPage = 1;
        this.onQuery();
      })
    },
    // 重置
    resetForm(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
        this.$refs[formName].clearValidate();
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.line {
  text-align: center;
}
.grid-content {
  margin-top: 20px;
  border-radius: 4px;
  text-align: center;
  min-height: 36px;
  line-height: 36px;
}
.bg-purple {
  background: #e5e9f2;
}
</style>

